<!--
	vMoney
-->
<?php
	/***************************************************
	 * View Declaration
	 ***************************************************/

		$header = array(
			'입수금 - 캐릭터',
			'캐릭터의 입수금을 관리합니다.'
		);

		$formOption01 = sprintf('/%s/%s/',$this->nUri['seg1'],$this->nUri['txSeg']);
		$formOption02 = array(
			'role' => 'form',
			'id' => 'nForm',
			'class' => 'nForm'
		);

		$pk_uid = array(
			'type' => 'hidden',
			'id' => 'pk_uid',
			'defaultValue' => $this->gibberishaes->enc(@$r['pk_uid'], $this->config->item('aes_key'))
		);

		$fk_characters_uid = array(
			'title' => '캐릭터',
			'id' => 'fk_characters_uid',
			'group' => @$g01,
			'defaultValue' => @$r['fk_characters_uid']
		);

		$fk_members_uid = array(
			'title' => '멤버',
			'id' => 'fk_members_uid',
			'items' => @$i01,
			'defaultValue' => @$r['fk_members_uid']
		);

		$fg_flow = array(
			'title' => '흐름',
			'id' => 'fg_flow',
			'items' => array(
				'gain' => '얻음',
				'lose' => '잃음'
			),
			'defaultValue' => @$r['fg_flow']
		);

		$no_money = array(
			'title' => '입수금',
			'type' => 'number',
			'labelText' => '',
			'id' => 'no_money',
			'placeholder' => '숫자를 입력하세요',
			'defaultValue' => @$r['no_money']
		);

		$tx_description = array(
			'title' => '비고',
			'labelText' => '',
			'id' => 'tx_description',
			'placeholder' => '비고를 입력하세요',
			'defaultValue' => @$r['tx_description']
		);

		$flowing = array(
			'title' => '흐름일시',
			'type' => 'datetime',
			'labelTextYMD' => '년월일',
			'labelTextHIS' => '시분초',
			'idYMD' => 'dt_flowing',
			'idHIS' => 'tm_flowing',
			'placeholderYMD' => '예시) 2014-01-01',
			'placeholderHIS' => '예시) 12:59:59',
			'defaultValueYMD' => @$r['dt_flowing'],
			'defaultValueHIS' => @$r['tm_flowing']
		);

		$nButtons01 = array(
			'layout' => 'horizontal-justified',
			'items' => array(
				array(
					'type' => 'primary',
					'text' => '확인',
					'class' => 'uIdBtnConfirm01'
				),
				array(
					'type' => 'default',
					'text' => '취소',
					'class' => 'uIdBtnCancel01'
				)
			)
		);

		$nButtons02 = array(
			'layout' => 'horizontal-justified',
			'items' => array(
				array(
					'type' => 'primary',
					'text' => '정산',
					'class' => 'uIdBtnConfirm02'
				),
				array(
					'type' => 'default',
					'text' => '취소',
					'class' => 'uIdBtnCancel01'
				)
			)
		);
		/* 검색박스 */
		$nSearchBar01 = array(
			'selectId' => 'nSelect',
			'selectValId' => 'nSelectVal',
			'searchBtnId' => 'uIdSearchBtn01',
			'searchBtnWrapperId02' => 'uIdSearchBtnWrapper02',
			'selectWrapperId01' => 'uIdSelectWrapper01',
			'groupId' => 'fk_characters_uid',
			'searchBtnId02' => 'uIdSearchBtn02',
			'group' => @$g01,
			'optionItems' => array(
				'fk_characters_uid' => '캐릭터이름',
				'fg_flow' => '흐름'
			)

		);



		/* table */
		$tableOption01 = array(
			'th' => array ('번호','캐릭터이름','흐름','입수금','흐름 일시','비고'),
			'cols' => array ('width:70px','width:230px;','width:70px','width:90px;','width:140px',''),
			'uId' => 'uIdTable01'
		);

		$nToolbarButton01 = array(
			'text' => '새로운 레코드 생성',
			'type' => 'primary',
			'icon' => 'fa fa-plus',
			'class' => 'uIdNewRecord01',
			'link' => $this->nmover->jsLink(
				array(
					'seg1' => $this->nUri['seg1'],
					'seg2' => $this->nUri['createSeg'],
					'queryString' => array( 'mode'=>'create' ))
				)
		);

		$nToolbarButton02 = array(
			'text' => '목록으로',
			'type' => 'primary',
			'icon' => 'fa fa-arrow-left',
			'class' => 'uIdGoToList01',
			'link' => $this->nmover->jsBack()
		);

		$nToolbarButton03 = array(
			'text' => '삭제',
			'type' => 'default',
			'icon' => 'fa fa-trash-o',
			'class' => 'uIdDelete pull-right',
			'link' => 'void(0)'
		);

		$nToolbarButton04 = array(
			'text' => '사냥끝 정산',
			'type' => 'primary',
			'icon' => 'fa fa-bank',
			'class' => 'uIdHuntingEndCalculation',
			'link' => $this->nmover->jsLink(
				array(
					'seg1' => $this->nUri['seg1'],
					'seg2' => $this->nUri['createSeg'],
					'queryString' => array( 'mode'=>'hunting_end_calculate' ))
				)
		);
?><?php
	/***************************************************
	 * View Output
	 ***************************************************/
?>
	<?=$this->nhtml->nHeader($header);?>
	<div class="main-content">
		<?=$this->nhtml->nFormOpen($formOption01, $formOption02)?>

			<?php
				/***************************************************
	 			* 목록
	 			***************************************************/
			?>
			<?php if($mode == 'list'): ?>
				<?=$this->nhtml->nSubjection('목록')?>
				<?=$this->nhtml->nHr()?>
				<?=$this->nhtml->nBtnToolbarOpen(array('size'=>'12'))?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton01)?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton04)?>
					<?=$this->nhtml->nSearchBarBType($nSearchBar01)?>
				<?=$this->nhtml->nBtnToolbarClose()?>

				<?=$this->nhtml->paginationContainer()?>
				<?=$this->nhtml->tableContainer($tableOption01);?>
				<?=$this->nhtml->paginationContainer()?>
			<?php endif;?>

			<?php
				/***************************************************
	 			* 생성, 조회
	 			***************************************************/
			?>
			<?php if($mode == 'create' || $mode == 'retrieve') : ?>
				<?=$this->nhtml->nSubjection($subjection)?>
				<?=$this->nhtml->nHr()?>
				<?=$this->nhtml->nBtnToolbarOpen(array('size'=>'12'))?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton02)?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton03)?>
				<?=$this->nhtml->nBtnToolbarClose()?>

				<?=$this->nhtml->nHidden($pk_uid)?>
				<?=$this->nhtml->nSelect($fk_characters_uid)?>
				<?=$this->nhtml->nRadio($fg_flow)?>
				<?=$this->nhtml->nInput($no_money)?>
				<?=$this->nhtml->nDateTimeInput($flowing)?>
				<?=$this->nhtml->nTextarea($tx_description)?>
				<?=$this->nhtml->nHr()?>
				<?=$this->nhtml->nButtons($nButtons01)?>
				<?php if($mode == 'create'):?>
					<script type="text/javascript" charset="utf-8">
						$('.uIdDelete').remove();
					</script>
				<?php endif;?>
			<?php endif;?>

			<?php if($mode == 'hunting_end_calculate') : ?>
				<?=$this->nhtml->nSubjection($subjection)?>
				<?=$this->nhtml->nHr()?>
				<?=$this->nhtml->nBtnToolbarOpen(array('size'=>'12'))?>
					<?=$this->nhtml->nToolbarButton($nToolbarButton02)?>
				<?=$this->nhtml->nBtnToolbarClose()?>

				<?=$this->nhtml->nSelect($fk_characters_uid)?>
				<?=$this->nhtml->nSelect($fk_members_uid)?>
				<?=$this->nhtml->nHr(array('option'=>'blank'))?>
				<?=$this->nhtml->nButtons($nButtons02)?>
			<?php endif;?>


		<?=$this->nhtml->nFormClose()?>
<?php
	/***************************************************
	 * View Ajax
	 ***************************************************/
?>
		<script type="text/javascript">
			var postValue = {};
			var postValueR01 = {};

			$(function(){


				/***************************************************
	 			* 암호화 키 바인딩
	 			***************************************************/
				postValue['GAesKey'] = GAesKey;


				/***************************************************
	 			* POST 값 바인딩
	 			***************************************************/
				<?=$this->nhtml->getAjaxNameValues('nameValuesPref')?>


				/***************************************************
	 			* 생성,수정
	 			***************************************************/
				<?php
					$sendMode = $mode;
					if($mode == 'retrieve'){
						$sendMode = 'update';
					}
				?>
				$('.uIdBtnConfirm01').click(function(){
					var targetButton = $(this);
					$(targetButton).addClass('disabled');
					var formAjaxSendPref = {
						oForm : '#nForm',
						mode : '<?=$this->nurl->encrypt($sendMode)?>',
						nameValues : nameValuesPref,
						afterCallback:function(data){
							if(data.process == 'validation' && data.resultMessage == 'fail'){
								//console.log('view >> validation, fail');
								$('.nValidation').removeClass('has-error');
								var vStatArray = jQuery.parseJSON(data.vStatArray);
								for(var i = 0; i < vStatArray.length ; i ++){
									var columnName = vStatArray[i]['columnName'];
									$('[name='+columnName+']').closest('.nValidation').addClass('has-error');
									//unique
									if(columnName == '<?=$this->nurl->encrypt("uq_name")?>'){
										//console.log('uq');
										$('[name='+columnName+']').siblings('.input-group-addon').html('<span>중복됨</span>');
									}
								}
								bootbox.alert('유효하지 않은 입력입니다. 입력한 데이터를 확인하세요.', function(){
									$(targetButton).removeClass('disabled');
									return;
								});
								return;
							}

							if(data.resultMessage == 'succ' && data.process == 'update'){
								bootbox.alert('데이터가 올바르게 수정 되었습니다.', function(){
									$(targetButton).removeClass('disabled');
									return;
								});
							}
							if(data.resultMessage == 'succ' && data.process == 'create'){
								bootbox.alert('데이터가 올바르게 생성 되었습니다.', function(){
									$(targetButton).removeClass('disabled');
									history.go(-1);
									return;
								});
							}
							$('.nValidation').removeClass('has-error');

						}
					};
					//console.log('sendmode >> <?=$sendMode?>');
					Najax.init(formAjaxSendPref);
					Najax.call();
				});


				/***************************************************
	 			* 삭제
	 			***************************************************/
				$('.uIdDelete').click(function(){
					var targetButton = $(this);
					$(targetButton).addClass('disabled');
					bootbox.confirm("정말로 데이터를 삭제 하시겠습니까?", function(result) {
						if(result){
							var formAjaxSendPref = {
								oForm : '#nForm',
								mode : '<?=$this->nurl->encrypt("delete")?>',
								nameValues : nameValuesPref,
								afterCallback:function(data){
									//console.log(data);
									$(targetButton).removeClass('disabled');
									if(data.resultMessage == 'succ'){
										//Nlog.put('delete');
										location.href ="/<?=$this->nUri['seg1']?>/<?=$this->nUri['seg2']?>";
									}
								}
							};
							Najax.init(formAjaxSendPref);
							Najax.call();
						}
					});
				});


				/***************************************************
	 			* 목록
	 			***************************************************/
				var listAjaxCallPref = {
					oForm : '#nForm',
					mode : '<?=$this->nurl->encrypt($mode)?>',
					afterCallback:function(data){
						$('.uIdTable01 tbody').html(jsonToHtml.getListData(data));
						$('.nPagination').html(data.pagination);
					}
				};
				Najax.init(listAjaxCallPref);
				Najax.r01('<?=base_url()?><?=$this->nUri["seg1"]?>/<?=$this->nUri["ntxSeg"]?>/?p1=0');

				/* pagination link build */
				var nPaginationP1 = '?p1=0';
				$(document).on('click', '.nPaginationPt', function(event) {
					var dp = $(event.currentTarget).attr('data-nPaginationVal');
					//console.log(dp);
					nPaginationP1 = '?'+dp;
					$('.uIdSearchBtn01').click();
				});


				/***************************************************
	 			* 검색
	 			***************************************************/
				$(document).on('focus blur change','.nSelect',function(){
					var selectedVal = $('.nSelect option:selected').val();
					if(selectedVal == '<?=$this->nurl->encrypt("fk_characters_uid")?>'){
						//console.log(selectedVal);
						$('.uIdSearchBtn01').add('.nSelectVal').attr('style','display:none !important');
						$('.uIdSearchBtnWrapper02').add('.uIdSelectWrapper01').attr('style','display:block !important');
					} else {
						$('.uIdSearchBtn01').add('.nSelectVal').attr('style','display:block !important');
						$('.uIdSearchBtnWrapper02').add('.uIdSelectWrapper01').attr('style','display:none !important');
					}
				});

				$('.uIdSearchBtn01').click(function(){
					var nSelectKey = '';
					var nSelectVal = '';
					postValueR01['GAesKey'] = GAesKey;
					nSelectKey = $('.nSelect option:selected').val();
					nSelectVal = $.trim(GAes.enc($('.nSelectVal').val(), GAesKey));
					//console.log('uIdSearchBtn01 clicked');
					//console.log('uIdSearchBtn01 clicked >> key>>' + nSelectKey + '>>val>>' + nSelectVal);
					var listAjaxCallPref = {
						oForm : '#nForm',
						mode : '<?=$this->nurl->encrypt($mode)?>',
						afterCallback:function(data){
							$('.nTable tbody').html(jsonToHtml.getListData(data));
							$('.nPagination').html(data.pagination);
						}
					};
					Najax.init(listAjaxCallPref);
					//console.log('data');
					Najax.r01('<?=base_url()?><?=$this->nUri["seg1"]?>/<?=$this->nUri["ntxSeg"]?>/'+nPaginationP1+'&<?=trim($this->nurl->encrypt("whereKey"))?>='+nSelectKey+'&<?=trim($this->nurl->encrypt("whereVal"))?>='+nSelectVal);
				});

				$('.uIdSearchBtn02').click(function(){
					var nSelectKey = '';
					var nSelectVal = '';
					postValueR01['GAesKey'] = GAesKey;
					nSelectKey = $('.nSelect option:selected').val();
					nSelectVal = $.trim(GAes.enc($('.uIdSelectWrapper01 select option:selected').val(), GAesKey));
					//console.log('uIdSearchBtn01 clicked');
					//console.log('uIdSearchBtn01 clicked >> key>>' + nSelectKey + '>>val>>' + nSelectVal);
					var listAjaxCallPref = {
						oForm : '#nForm',
						mode : '<?=$this->nurl->encrypt($mode)?>',
						afterCallback:function(data){
							$('.nTable tbody').html(jsonToHtml.getListData(data));
							$('.nPagination').html(data.pagination);
						}
					};
					Najax.init(listAjaxCallPref);
					//console.log('data');
					Najax.r01('<?=base_url()?><?=$this->nUri["seg1"]?>/<?=$this->nUri["ntxSeg"]?>/'+nPaginationP1+'&<?=trim($this->nurl->encrypt("whereKey"))?>='+nSelectKey+'&<?=trim($this->nurl->encrypt("whereVal"))?>='+nSelectVal);
				});



				/***************************************************
	 			* 정산
	 			***************************************************/
				$('.uIdBtnConfirm02').click(function(){
					var targetButton = $(this);
					$(targetButton).addClass('disabled');
					var formAjaxSendPref = {
						oForm : '#nForm',
						mode : '<?=$this->nurl->encrypt("hunting_end_calculate")?>',
						nameValues : nameValuesPref,
						afterCallback:function(data){

							if(data.resultMessage == 'succ' && data.process == 'hunting_end_calculate'){
								bootbox.alert('정산이 완료 되었습니다.', function(){
									$(targetButton).removeClass('disabled');
									return;
								});
							}

							if(data.resultMessage == 'fail' && data.process == 'hunting_end_calculate'){
								bootbox.alert('정산에 실패 하였습니다.', function(){
									$(targetButton).removeClass('disabled');
									return;
								});
							}
							$('.nValidation').removeClass('has-error');
						}
					};
					//console.log('sendmode >> <?=$sendMode?>');
					Najax.init(formAjaxSendPref);
					Najax.call();
				});

			});
		</script>
